package ${package_name}.service.impl;

import ${package_name}.dao.${class_name}Dao;
import ${package_name}.${class_package}.${class_name}Po;
import ${package_name}.service.${class_name}Service;
import ${package_name}.vo.${class_name}Vo;
import lombok.extern.slf4j.Slf4j;
import com.jinhua.config.BeanCopierUtil;
import com.jinhua.config.IdUtil;
import com.jinhua.config.PaginationRequest;
import com.jinhua.config.PaginationResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

/**
* ${entity_name}的service层
**/
@Service
@Slf4j
public class ${class_name}ServiceImpl implements ${class_name}Service {
private final long ERROR_LONG = 0L;
private final int ERROR_INT = 0;
@Autowired
p4jprivate ${class_name}Dao ${lower_class_name}Dao;

@Transactional
@Override
p4jpublic long save${class_name}(${class_name}Vo ${lower_class_name}Vo) {

if (${lower_class_name}Vo == null) {
log.warn("操作${entity_name}参数为空");
return ERROR_LONG;
}
${class_name}Po ${lower_class_name}Po = ${lower_class_name}Vo.copy${class_name}Po();

if (${lower_class_name}Po.getId() == null) {
log.info("start添加${entity_name}={}", ${lower_class_name}Po);
int result = ${lower_class_name}Dao.save${class_name}IfNotExist(${lower_class_name}Po);
return result != 0 ? ${lower_class_name}Po.getId() : ERROR_LONG;
} else {
log.info("start修改${entity_name}={}", ${lower_class_name}Po.getId());

return ${lower_class_name}Dao.mod${class_name}(${lower_class_name}Po);
}

}


@Override
p4jpublic int del${class_name}ByIds(Long... ids) {
if (ids == null || ids.length == 0) {
log.warn("删除${entity_name},ids集合为空");
return ERROR_INT;
}

log.warn("start删除Id集合={},${entity_name}", Arrays.asList(ids));

return ${lower_class_name}Dao.del${class_name}ByIds(ids);
}

@Override
p4jpublic ${class_name}Vo find${class_name}ById(Long id) {

if (id == null||id<=0L) {
log.warn("根据Id查找${entity_name},所传Id不符合规范");
return new ${class_name}Vo();
}
log.info("start根据Id={}查找${entity_name}", id);


${class_name}Po ${lower_class_name}Po = ${lower_class_name}Dao.find${class_name}ById(id);


return ${lower_class_name}Po==null?new ${class_name}Vo():${lower_class_name}Po.copy${class_name}Vo() ;

}


@Override
p4jpublic PaginationResponse<${class_name}Vo> find${class_name}ByAll(PaginationRequest<${class_name}Vo> request) {
        PaginationResponse<${class_name}Vo> response = new PaginationResponse<${class_name}Vo>();

        if (request == null) {
        log.warn("根据条件查找${entity_name},参数不对");
        return response;
        }
        log.info("start根据条件查找${entity_name}={}", request);
        List<${class_name}Vo> ${lower_class_name}Vos = new ArrayList<>();
        try {
            List<${class_name}Po> ${lower_class_name}Pos = ${lower_class_name}Dao.find${class_name}ByAll(
                request.getQueryCriteria().copy${class_name}Po(),request.getPageNum(),request.getPageSize());
                ${lower_class_name}Vos = BeanCopierUtil.copyList(${lower_class_name}Pos,${class_name}Po.class, ${class_name}Vo.class);
            } catch (Exception e) {
            log.error("${class_name}ServiceImpl.findOrderByAll error:{}",e.getMessage(), e);
                throw new RuntimeException(e);
            } response.setDataList(${lower_class_name}Vos);
                        response.setPageSize(request.getPageSize());
                        response.setPageNum(request.getPageNum());
                        response.setTotalCount(IdUtil.getTotal());
            return response;
            }

}
